package com.pzhyd.sc.biz.impl;

import java.util.ArrayList;
import java.util.List;

import com.pzhyd.sc.biz.inter.IroleBiz;
import com.pzhyd.sc.dao.inter.IBaseDao;
import com.pzhyd.sc.entity.Roleinfo;
import com.pzhyd.sc.util.Page;

/** 
* @Project: webvote
* @Title: RoleBizImpl.java
* @Package com.pzhyd.sc.biz.impl
* @Description: 角色管理业务逻辑层
* @author Gaoshiwei 812257531@qq.com
* @date Jul 9, 2012 2:38:14 PM
* @Copyright: 2012 攀枝花移动 All rights reserved.
* @version V1.0  
 */
public class RoleBizImpl implements IroleBiz {
	
	private IBaseDao idao;

	public IBaseDao getIdao() {
		return idao;
	}

	public void setIdao(IBaseDao idao) {
		this.idao = idao;
	}

	public int createRole(Roleinfo role) {
		// TODO Auto-generated method stub
		int count = 0;
		try {
			count = idao.insert(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	/**
	 * 批量删除角色
	 */
	public int deleteRoleByRids(int[] rid) {
		// TODO Auto-generated method stub
		int count=0;
		StringBuilder sbStr = new StringBuilder();
		for (int i=0; i<rid.length; i++) {
			sbStr.append("?");
			if (i < (rid.length - 1)) {
				sbStr.append(",");
			}
		}
		List params = new ArrayList();
		for (int i=0; i<rid.length; i++) {
			params.add(rid[i]);
		}
		
		 String hql = "delete from Roleinfo where rid in (" + sbStr.toString()  +  ")";
		 try {
			count=idao.operateHql(hql, params);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}

	public void serachAll(Page page) {
		// TODO Auto-generated method stub
		String hql1 = "select count(*) from Roleinfo";
		String hql = "from Roleinfo r order by r.rid";
		try {
			page.setTotalRecords(idao.getTotalCount(hql1, null));
			idao.searchHql(hql, null, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Roleinfo> serachAll() {
		// TODO Auto-generated method stub
		String hql = "from Roleinfo r order by r.rid";
		List<Roleinfo> lst = null;
		try {
			lst = idao.searchHql(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lst;
	}

	public Roleinfo serachByRid(String rid) {
		// TODO Auto-generated method stub
		String hql = "from Roleinfo r where r.rid = ?";
		List parms = new ArrayList();
		parms.add(new Integer(rid));//如果表中是int型，就需要将string进行转换
		Roleinfo role = null;
		try {
		  List lt=idao.searchHql(hql, parms);
		  if(lt.size()>0){
			  role=(Roleinfo)lt.get(0);
		  }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return role;
	}

	public List<Roleinfo> serachByRname(String rname) {
		// TODO Auto-generated method stub
		String hql = "from Roleinfo r where r.rname = ?";
		List parms = new ArrayList();
		parms.add(rname);
		List<Roleinfo> role = null;
		try {
			role = idao.searchHql(hql, parms);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("RoleBiz中的值"+role);
		return role;
	}

	public int updateRole(Roleinfo role) {
		// TODO Auto-generated method stub
		int count = 0;
		try {
			count = idao.update(role);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return count;
	}

	public List<Roleinfo> searchBypid(String pid) {
		// TODO Auto-generated method stub
		String hql = "from Roleinfo r where r.rparentId= ?";
		List parms = new ArrayList();
		parms.add(new Integer(pid));//如果表中是int型，就需要将string进行转换
		List lt=null;
		try {
		 lt=idao.searchHql(hql, parms);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lt;
	}
}